package com.cheng.controller;

import com.cheng.pojo.AppVersion;
import com.cheng.pojo.DevUser;
import com.cheng.service.appversion.AppVersionService;
import com.cheng.utils.UUIDutils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.cs.ext.MS874;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@SuppressWarnings("all")
@RequestMapping(value = "/ver")
public class AppVersionController {
    @Autowired
    private AppVersionService appVersionService;

    @RequestMapping(value = "/addPage")
    public String addPage(int id, Model model,
                          @RequestParam(value = "msg", required = false) String msg) {
        List<AppVersion> list = appVersionService.getHistoryVersionById(id);
        model.addAttribute("appVersionList", list);
        model.addAttribute("appId", id);

        if ("addFail".equals(msg)) {
            msg = "添加失败";
        } else if ("fileType".equals(msg)) {
            msg = "文件类型不正确";
        } else if ("noFile".equals(msg)) {
            msg = "未选择文件";
        }

        model.addAttribute("msg", msg);
        return "developer/appversionadd";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(AppVersion appVersion, MultipartFile a_downloadLink, HttpSession session) {
        String realPath = "E:\\code\\JavaIDEA\\练习\\AppManagement\\src\\main\\webapp\\statics\\uploadfiles";
        String msg = "";
        if (!a_downloadLink.isEmpty()) {
            String filename = a_downloadLink.getOriginalFilename();
            String extension = FilenameUtils.getExtension(filename);
            if (extension.equalsIgnoreCase("apk")) {
                filename = UUIDutils.getUUID() + "." + extension;
                try {
                    a_downloadLink.transferTo(new File(realPath, filename));

                    DevUser user = (DevUser) session.getAttribute("devUserSession");
                    appVersion.setCreatedBy(user.getId());
                    appVersion.setDownloadLink("statics/uploadfiles/" + filename);
                    appVersion.setApkLocPath(realPath + "/" + filename);
                    appVersion.setApkFileName(filename);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                int count = appVersionService.addVersion(appVersion);
                if (count > 0) {
                    return "redirect:/ver/addPage?id=" + appVersion.getAppId();
                } else {
                    msg = "addFail";
                }
            } else {
                msg = "fileType";
            }

        } else {
            msg = "noFile";
        }
        return "redirect:/ver/addPage?id=" + appVersion.getAppId() + "&msg=" + msg;
    }
}
